from flask import session

from common.enums import employeeType, Role
from util.db import Cursor


class UserModal:

    @property
    def current_user(self):
        user_id = session.get('user_id')
        sql = """select id, username, email, phone from `yugong_user` where `id`=%s"""
        with Cursor() as cursor:
            cursor.execute(sql, [user_id])
            res = cursor.fetchall()[0]
        return res

    @property
    def username(self):
        return self.current_user.get('username')
    @staticmethod
    def get_user_info():
        user_id = session.get('user_id')
        username = session.get('username')
        with Cursor() as cursor:
            sql = """select * from `yugong_user` where `id`=%s"""
            cursor.execute(sql, [user_id])
            res = cursor.fetchall()[0]
            user_info = {
                'loginStatus': session.get('login_status'),
                "avatar": res.get('avatar'),
                "id": user_id,
                "username": res.get('username'),
                "department": res.get('department'),
                "employeeType": employeeType.get(res.get('employeeType')),
                "role": Role.get(res.get('role')),
                "job": res.get('job'),
                "probationStart": "2021-04-19",
                "probationEnd": "2021-10-15",
                "probationDuration": "180",
                "protocolStart": "2021-04-19",
                "protocolEnd": "2024-04-19",
                "address": res.get('address'),
                "status": res.get('status'),
                "email": res.get('email'),
                "phone": res.get('phone'),
                "description": res.get('description')
            }
            return user_info

    @staticmethod
    def get_user_name_by_id(user_id):
        with Cursor() as cursor:
            sql = """select username from `yugong_user` where `id`=%s"""
            cursor.execute(sql, [user_id])
            res = cursor.fetchall()
            if res:
                return res[0]['username']
            return user_id
